package top.kaoshanji.ch06;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import top.kaoshanji.ch06.proxy.TraceHandler;

import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.Random;

/**
 * 代码清单 ch06-03：代理示例(6.5节)
 * author kaoshanji
 * ^_^
 * create time 2024/11/27 16:31
 */
public class ProxyTest {

    private static final Logger logger = LogManager.getLogger(ProxyTest.class);

    public static void main(String[] args) {
        logger.info("...代码清单 ch06-03：代理示例(6.5节)...");

        var elements = new Object[1000];

        for (int i = 0; i < elements.length; i++) {
            Integer value = i + 1;
            var handler = new TraceHandler(value);

            Object proxy = Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]{Comparable.class}, handler);

            elements[i] = proxy;
        }

        // construct a random integer
        Integer key = new Random().nextInt(elements.length) + 1;

        // search for the key
        int result = Arrays.binarySearch(elements, key);

        // print match if found
        if (result >= 0) System.out.println(elements[result]);

    }

}
